package ele_handler

import (
	"fmt"
	"go-yuque/logger"
	"golang.org/x/net/html"
	"strings"
)

func init() {
	registerInitFunc(
		func() YuqueElementHandler {
			h := &codeBlockHandlerStruct{}
			// h.BaseHandler.self = h
			h.elements = []string{"card"}
			h.attributes = map[string]string{"name": "codeblock"}
			return h
		})
}

type codeBlockHandlerStruct struct {
	BaseHandler
	Search        string        `json:"search"`
	Mode          string        `json:"mode"`
	Code          string        `json:"code"`
	AutoWrap      bool          `json:"autoWrap"`
	LineNumbers   bool          `json:"lineNumbers"`
	HeightLimit   bool          `json:"heightLimit"`
	Collapsed     bool          `json:"collapsed"`
	HideToolbar   bool          `json:"hideToolbar"`
	Name          string        `json:"name"`
	TabSize       interface{}   `json:"tabSize"`
	IndentWithTab bool          `json:"indentWithTab"`
	LightLines    []interface{} `json:"lightLines"`
	FoldLines     []interface{} `json:"foldLines"`
	Theme         string        `json:"theme"`
	FontSize      int           `json:"fontSize"`
	CustomStyle   []interface{} `json:"customStyle"`
	Spacing       string        `json:"__spacing"`
	ID            string        `json:"id"`
	Margin        struct {
		Top    bool `json:"top"`
		Bottom bool `json:"bottom"`
	} `json:"margin"`
}

func (cb *codeBlockHandlerStruct) doHandle() error {

	err := parseCardJson(cb.node, cb)
	if err != nil {
		logger.Errorf("[codeBlock]解析异常")
		sb := strings.Builder{}
		err := html.Render(&sb, cb.node)
		if err != nil {
			cb.WriteTextContext(sb.String())
		}
		return err
	}

	mode, ok := modeMap[cb.Mode]
	if !ok {
		mode = ""
	}
	cb.WriteTextContext(fmt.Sprintf("```%s\n", mode))
	cb.WriteTextContext(fmt.Sprintf("%s\n", cb.Code))
	cb.WriteTextContext("```\n")

	return nil
}

// 代码块 模式名称与 md 代码块语言对应关系
var modeMap = map[string]string{
	"agda":          "Agda",
	"arkts":         "ArkTS",
	"z80":           "Asm",
	"bash":          "Bash",
	"basic":         "Basic",
	"c":             "C",
	"cpp":           "C++",
	"csharp":        "C#",
	"css":           "CSS",
	"dart":          "Dart",
	"diff":          "Diff",
	"dockerfile":    "Dockerfile",
	"erlang":        "Erlang",
	"glsl":          "Glsl",
	"git":           "Git",
	"go":            "Go",
	"graphql":       "GraphQL",
	"groovy":        "Groovy",
	"html":          "HTML",
	"http":          "HTTP",
	"java":          "Java",
	"javascript":    "JavaScript",
	"json":          "JSON",
	"jsx":           "JSX",
	"katex":         "KaTeX",
	"kotlin":        "Kotlin",
	"less":          "Less",
	"makefile":      "Makefile",
	"markdown":      "Markdown",
	"matlab":        "MATLAB",
	"nginx":         "Nginx",
	"objectivec":    "Objective-C",
	"pascal":        "Pascal",
	"perl":          "Perl",
	"php":           "PHP",
	"powershell":    "PowerShell",
	"protobuf":      "Protobuf",
	"python":        "Python",
	"r":             "R",
	"ruby":          "Ruby",
	"rust":          "Rust",
	"scala":         "Scala",
	"shell":         "Shell",
	"sql":           "SQL",
	"plsql":         "PL/SQL",
	"swift":         "Swift",
	"typescript":    "TypeScript",
	"vbnet":         "VB.net",
	"velocity":      "Velocity",
	"xml":           "XML",
	"yaml":          "YAML",
	"stex":          "sTeX",
	"latex":         "LaTeX",
	"systemverilog": "SystemVerilog",
	"sass":          "Sass",
	"tcl":           "Tcl",
	"verilog":       "Verilog",
	"vue":           "Vue",
	"lua":           "Lua",
	"haskell":       "Haskell",
	"properties":    "Properties",
	"toml":          "TOML",
	"cypher":        "Cypher",
	"tsx":           "TSX",
	"f#":            "F#",
	"ocaml":         "OCaml",
	"clojure":       "Clojure",
	"abap":          "ABAP",
	"julia":         "Julia",
	"cmake":         "CMake",
	"scheme":        "Scheme",
	"commonlisp":    "Lisp",
	"fortran":       "Fortran",
	"solidity":      "Solidity",
}

/**
// 代码块 模式名称与 md 代码块语言对应关系
[
  {
    "value": "agda",
    "syntax": "text/x-agda",
    "name": "Agda",
    "ext": ["agda"]
  },
  {
    "value": "arkts",
    "syntax": "text/x-arkts",
    "name": "ArkTS",
    "ext": ["ets", "arkts"]
  },
  {
    "value": "z80",
    "syntax": "z80",
    "name": "Asm",
    "ext": ["asm"]
  },
  {
    "value": "bash",
    "syntax": "shell",
    "name": "Bash",
    "ext": ["bash"]
  },
  {
    "value": "basic",
    "syntax": "vbscript",
    "name": "Basic",
    "ext": ["vbs"]
  },
  {
    "value": "c",
    "syntax": "text/x-csrc",
    "name": "C",
    "ext": ["c", "h", "ino"]
  },
  {
    "value": "cpp",
    "syntax": "text/x-c++src",
    "name": "C++",
    "ext": ["cpp", "c++", "cc", "cxx", "hpp", "h++", "hh", "hxx"]
  },
  {
    "value": "csharp",
    "syntax": "text/x-csharp",
    "name": "C#",
    "ext": ["cs"]
  },
  {
    "value": "css",
    "syntax": "css",
    "name": "CSS",
    "ext": ["css"]
  },
  {
    "value": "dart",
    "syntax": "dart",
    "name": "Dart",
    "ext": ["dart"]
  },
  {
    "value": "diff",
    "syntax": "diff",
    "name": "Diff",
    "ext": ["diff", "patch"]
  },
  {
    "value": "dockerfile",
    "syntax": "dockerfile",
    "name": "Dockerfile"
  },
  {
    "value": "erlang",
    "syntax": "erlang",
    "name": "Erlang",
    "ext": ["erl"]
  },
  {
    "value": "glsl",
    "syntax": "x-shader/x-vertex",
    "name": "Glsl",
    "ext": ["glsl"]
  },
  {
    "value": "git",
    "syntax": "shell",
    "name": "Git"
  },
  {
    "value": "go",
    "syntax": "go",
    "name": "Go",
    "ext": ["go"]
  },
  {
    "value": "graphql",
    "syntax": "graphql",
    "name": "GraphQL"
  },
  {
    "value": "groovy",
    "syntax": "groovy",
    "name": "Groovy",
    "ext": ["groovy", "gradle"]
  },
  {
    "value": "html",
    "syntax": "htmlmixed",
    "name": "HTML",
    "ext": ["html", "htm", "handlebars", "hbs"]
  },
  {
    "value": "http",
    "syntax": "http",
    "name": "HTTP"
  },
  {
    "value": "java",
    "syntax": "text/x-java",
    "name": "Java",
    "ext": ["java"]
  },
  {
    "value": "javascript",
    "syntax": "text/javascript",
    "name": "JavaScript",
    "ext": ["js"]
  },
  {
    "value": "json",
    "syntax": "application/json",
    "name": "JSON",
    "ext": ["json", "map"]
  },
  {
    "value": "jsx",
    "syntax": "jsx",
    "name": "JSX",
    "ext": ["jsx"]
  },
  {
    "value": "katex",
    "syntax": "simplemode",
    "name": "KaTeX"
  },
  {
    "value": "kotlin",
    "syntax": "text/x-kotlin",
    "name": "Kotlin",
    "ext": ["kt"]
  },
  {
    "value": "less",
    "syntax": "css",
    "name": "Less",
    "ext": ["less"]
  },
  {
    "value": "makefile",
    "syntax": "cmake",
    "name": "Makefile"
  },
  {
    "value": "markdown",
    "syntax": "markdown",
    "name": "Markdown",
    "ext": ["markdown", "md", "mkd"]
  },
  {
    "value": "matlab",
    "syntax": "octave",
    "name": "MATLAB"
  },
  {
    "value": "nginx",
    "syntax": "nginx",
    "name": "Nginx",
    "ext": ["conf"]
  },
  {
    "value": "objectivec",
    "syntax": "text/x-objectivec",
    "name": "Objective-C",
    "ext": ["m"]
  },
  {
    "value": "pascal",
    "syntax": "pascal",
    "name": "Pascal",
    "ext": ["p", "pas"]
  },
  {
    "value": "perl",
    "syntax": "perl",
    "name": "Perl",
    "ext": ["pl", "pm"]
  },
  {
    "value": "php",
    "syntax": "text/x-php",
    "name": "PHP",
    "ext": ["php", "php3", "php4", "php5", "php7", "phtml"]
  },
  {
    "value": "powershell",
    "syntax": "powershell",
    "name": "PowerShell",
    "ext": ["ps1", "psd1", "psm1"]
  },
  {
    "value": "protobuf",
    "syntax": "protobuf",
    "name": "Protobuf",
    "ext": ["proto"]
  },
  {
    "value": "python",
    "syntax": "python",
    "name": "Python",
    "ext": ["build", "bzl", "py", "pyw"]
  },
  {
    "value": "r",
    "syntax": "r",
    "name": "R",
    "ext": ["r", "R"]
  },
  {
    "value": "ruby",
    "syntax": "ruby",
    "name": "Ruby",
    "ext": ["rb"]
  },
  {
    "value": "rust",
    "syntax": "rust",
    "name": "Rust",
    "ext": ["rs"]
  },
  {
    "value": "scala",
    "syntax": "text/x-scala",
    "name": "Scala",
    "ext": ["scala"]
  },
  {
    "value": "shell",
    "syntax": "shell",
    "name": "Shell",
    "ext": ["sh", "ksh"]
  },
  {
    "value": "sql",
    "syntax": "text/x-sql",
    "name": "SQL",
    "ext": ["sql"]
  },
  {
    "value": "plsql",
    "syntax": "text/x-plsql",
    "name": "PL/SQL"
  },
  {
    "value": "swift",
    "syntax": "swift",
    "name": "Swift",
    "ext": ["swift"]
  },
  {
    "value": "typescript",
    "syntax": "text/typescript",
    "name": "TypeScript",
    "ext": ["ts"]
  },
  {
    "value": "vbnet",
    "syntax": "vb",
    "name": "VB.net",
    "ext": ["vb"]
  },
  {
    "value": "velocity",
    "syntax": "velocity",
    "name": "Velocity",
    "ext": ["vtl"]
  },
  {
    "value": "xml",
    "syntax": "xml",
    "name": "XML",
    "ext": ["xml", "xsl", "xsd", "svg"]
  },
  {
    "value": "yaml",
    "syntax": "yaml",
    "name": "YAML",
    "ext": ["yaml", "yml"]
  },
  {
    "value": "stex",
    "syntax": "text/x-stex",
    "name": "sTeX"
  },
  {
    "value": "latex",
    "syntax": "text/x-latex",
    "name": "LaTeX",
    "ext": ["text", "ltx", "tex"]
  },
  {
    "value": "systemverilog",
    "syntax": "text/x-systemverilog",
    "name": "SystemVerilog",
    "ext": ["sv", "svh"]
  },
  {
    "value": "sass",
    "name": "Sass",
    "syntax": "text/x-sass",
    "ext": ["sass", "scss"]
  },
  {
    "value": "tcl",
    "syntax": "text/x-tcl",
    "name": "Tcl",
    "ext": ["tcl"]
  },
  {
    "value": "verilog",
    "syntax": "text/x-verilog",
    "name": "Verilog",
    "ext": ["v"]
  },
  {
    "value": "vue",
    "syntax": "text/x-vue",
    "name": "Vue"
  },
  {
    "value": "lua",
    "syntax": "text/x-lua",
    "name": "Lua",
    "ext": ["lua"]
  },
  {
    "value": "haskell",
    "syntax": "haskell",
    "name": "Haskell",
    "ext": ["hs"]
  },
  {
    "value": "properties",
    "syntax": "properties",
    "name": "Properties",
    "ext": ["properties", "ini", "in"]
  },
  {
    "value": "toml",
    "syntax": "toml",
    "name": "TOML",
    "ext": ["toml"]
  },
  {
    "value": "cypher",
    "syntax": "cypher",
    "name": "Cypher",
    "ext": ["cyp", "cypher"]
  },
  {
    "value": "tsx",
    "syntax": "jsx",
    "name": "TSX",
    "ext": ["tsx"]
  },
  {
    "value": "f#",
    "syntax": "mllike",
    "name": "F#",
    "ext": ["fs"]
  },
  {
    "value": "ocaml",
    "syntax": "mllike",
    "name": "OCaml",
    "ext": ["ml", "mli", "mll", "mly"]
  },
  {
    "value": "clojure",
    "syntax": "clojure",
    "name": "Clojure",
    "ext": ["clj", "cljc", "cljx"]
  },
  {
    "value": "abap",
    "syntax": "abap",
    "name": "ABAP"
  },
  {
    "value": "julia",
    "syntax": "julia",
    "name": "Julia",
    "ext": ["jl"]
  },
  {
    "value": "cmake",
    "syntax": "cmake",
    "name": "CMake",
    "ext": ["cmake"]
  },
  {
    "value": "scheme",
    "syntax": "scheme",
    "name": "Scheme",
    "ext": ["scm", "ss"]
  },
  {
    "value": "commonlisp",
    "syntax": "commonlisp",
    "name": "Lisp",
    "ext": ["cl", "lisp", "el"]
  },
  {
    "value": "fortran",
    "syntax": "fortran",
    "name": "Fortran",
    "ext": ["f90", "f95", "f03"]
  },
  {
    "value": "solidity",
    "syntax": "solidity",
    "name": "Solidity",
    "ext": ["sol"]
  }
]

**/
